<?php
declare (strict_types = 1);

namespace app\mini\model;
use think\Model;


class MiniProgramFans extends Model
{
  /*更新用户信息*/
  public static function updateFansData(Int $fansId,Array $data): Void
  {
    self::update([
      'unionId'         =>  $data['unionid']??'',
      'nickName'        =>  $data['nickName'],
      'gender'          =>  $data['gender'],
      'country'         =>  $data['country'],
      'province'        =>  $data['province'],
      'city'            =>  $data['city'],
      'avatarUrl'       =>  $data['avatarUrl']
    ],['id'=>$fansId]);
  }

  /*检测openid是否存在 不存在则新增到数据库，登陆时使用*/
  public static function findOrCreat(String $openId,$data): Int
  {
    $fansId = self::where('openId','=',$openId)->value('id',0);
    if( $fansId == 0 ){
        $addData = [
                'openId'=>$openId,
                'phone'=>$data['phone'],
                'nickName'=>$data['nickName'],
                'telephone'=>$data['telephone'],
                'createTime'=>date('Y-m-d H:i:s')
            ];
      $res = self::create([]);
      $fansId = (int) $res->id;
    }
    return $fansId;
  }



}
